﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.ComponentModel.DataAnnotations;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Annotations
{
    /// <summary>
    /// 身份证验证
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class IdCardAttribute : ValidationAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value.IsEmpty()) return ValidationResult.Success;
            var result = IDCardHelper.Validate(value.ToStringOrEmpty());
            return result.Success ? ValidationResult.Success : new ValidationResult(ErrorMessage ?? result.Msg);
        }
    }
}